package com.ysd.twocrm.service.impl;

import com.ysd.twocrm.entity.EmailUtils;
import com.ysd.twocrm.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class MailServiceImpl implements MailService {
@Autowired
    private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
    private String from;
    @Override
    public void sendMail(EmailUtils em) {
   //创建邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//创建发送邮件工具类
        MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage);

        try {
            mimeMessageHelper.setSubject(em.getTitle());//标题
            mimeMessageHelper.setText(em.getContent(),true);//内容
            mimeMessageHelper.setTo(em.getTo());//被发送人
            mimeMessageHelper.setFrom(from);//发送人

            javaMailSender.send(mimeMessage);//执行发送方法
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
